package com.snail.snailvr.views.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.snail.snailvr.R;
import com.snail.snailvr.VRApp;
import com.snail.snailvr.widget.FSSimpleImageView;
import com.snail.snailvr.widget.RecyclingPagerAdapter;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Jason on 16/4/15
 */
public class FeedbackPreviewAdapter extends RecyclingPagerAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private List<Uri> images;

    public FeedbackPreviewAdapter(Context context, List<Uri> list) {
        mContext = context;
        inflater = LayoutInflater.from(VRApp.getContext());
        images = list;
    }

    @Override
    public View getView(int position, View view, ViewGroup container) {
        final int index = getPosition(position);

        final Uri uri = images.size() == 0 ? null : images.get(position);

        ViewHolder holder;
        if (view == null) {
            view = inflater.inflate(R.layout.banner_view, container,false);
            holder = new ViewHolder(view);
            holder.imageView = (FSSimpleImageView) view.findViewById(R.id.image_view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if (uri != null) {
            holder.imageView.setImageURI(uri);
        }
        return view;
    }

    @Override
    public int getCount() {
        if (images!=null && !images.isEmpty())
            return images.size();
        return 0;
    }

    @Override
    public int getCircleCount() {
        if (images != null)
            return images.size();
        return 0;
    }

    public Uri getItem(int position){
        if (images != null)
            return images.get(position);
        return null;
    }

    public void removeItem(int position){
        if ( images != null && images.size()>position) {
            images.remove(position);
            notifyDataSetChanged();
        }
    }

    public List<Uri> getImages() {
        return images;
    }

    /**
     * get really position
     *
     * @param position
     * @return
     */
    private int getPosition(int position) {
        return position % images.size();
    }

    // TODO: 16-6-8  
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    class ViewHolder {
        @Bind(R.id.image_view)
        FSSimpleImageView imageView;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            imageView.setImagePlaceHolder(R.drawable.ic_live_default);
        }
    }
}
